package com.example.common.utils.thread_pool;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author zwp14
 * @create 2024/6/21 16:03
 * @description springframework-提供的线程池
 */
public class TaskExecutorUtil {
    public static ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();

    public static ThreadPoolTaskExecutor getTaskExecutor(String name,Integer coreSize,Integer maxSize,
                                       Integer workQueueSize){
        taskExecutor.setThreadNamePrefix(name);
        taskExecutor.setCorePoolSize(coreSize);
        taskExecutor.setMaxPoolSize(maxSize);
        taskExecutor.setQueueCapacity(workQueueSize);
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        taskExecutor.initialize();
        return taskExecutor;
    }

    public static ThreadPoolTaskExecutor defaultTaskExecutor(){
        taskExecutor.setThreadNamePrefix("zwp-creat-thread-service-");
        taskExecutor.setCorePoolSize(5);
        taskExecutor.setMaxPoolSize(10);
        taskExecutor.setQueueCapacity(100);
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        taskExecutor.initialize();
        return taskExecutor;
    }

}
